home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / clang / gnumake.zip / MAIN.C < prev    next >
C/C++ Source or Header  |  1994-06-07  |  49KB  |  1,831 lines

  1. /* Argument parsing and main program of GNU Make.
  2. Copyright (C) 1988, 89, 90, 91, 94 Free Software Foundation, Inc.
  3. This file is part of GNU Make.
  4.  
  5. GNU Make is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9.  
  10. GNU Make is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. GNU General Public License for more details.
  14.  
  15. You should have received a copy of the GNU General Public License
  16. along with GNU Make; see the file COPYING.  If not, write to
  17. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  18.  
  19. #include "make.h"
  20. #include "commands.h"
  21. #include "dep.h"
  22. #include "file.h"
  23. #include "variable.h"
  24. #include "job.h"
  25. #include "getopt.h"
  26.  
  27.  
  28. extern void print_variable_data_base ();
  29. extern void print_dir_data_base ();
  30. extern void print_rule_data_base ();
  31. extern void print_file_data_base ();
  32. extern void print_vpath_data_base ();
  33.  
  34. #ifndef    HAVE_UNISTD_H
  35. extern int chdir ();
  36. #endif
  37. #ifndef    STDC_HEADERS
  38. #ifndef    sun            /* Sun has an incorrect decl in a header.  */
  39. extern void exit ();
  40. #endif
  41. extern double atof ();
  42. #endif
  43. extern char *mktemp ();
  44.  
  45. static void log_working_directory ();
  46. static void print_data_base (), print_version ();
  47. static void decode_switches (), decode_env_switches ();
  48. static void define_makeflags ();
  49.  
  50. /* The structure that describes an accepted command switch.  */
  51.  
  52. struct command_switch
  53.   {
  54.     char c;            /* The switch character.  */
  55.  
  56.     enum            /* Type of the value.  */
  57.       {
  58.     flag,            /* Turn int flag on.  */
  59.     flag_off,        /* Turn int flag off.  */
  60.     string,            /* One string per switch.  */
  61.     positive_int,        /* A positive integer.  */
  62.     floating,        /* A floating-point number (double).  */
  63.     ignore            /* Ignored.  */
  64.       } type;
  65.  
  66.     char *value_ptr;    /* Pointer to the value-holding variable.  */
  67.  
  68.     unsigned int env:1;        /* Can come from MAKEFLAGS.  */
  69.     unsigned int toenv:1;    /* Should be put in MAKEFLAGS.  */
  70.     unsigned int no_makefile:1;    /* Don't propagate when remaking makefiles.  */
  71.  
  72.     char *noarg_value;    /* Pointer to value used if no argument is given.  */
  73.     char *default_value;/* Pointer to default value.  */
  74.  
  75.     char *long_name;        /* Long option name.  */
  76.     char *argdesc;        /* Descriptive word for argument.  */
  77.     char *description;        /* Description for usage message.  */
  78.   };
  79.  
  80.  
  81. /* The structure used to hold the list of strings given
  82.    in command switches of a type that takes string arguments.  */
  83.  
  84. struct stringlist
  85.   {
  86.     char **list;    /* Nil-terminated list of strings.  */
  87.     unsigned int idx;    /* Index into above.  */
  88.     unsigned int max;    /* Number of pointers allocated.  */
  89.   };
  90.  
  91.  
  92. /* The recognized command switches.  */
  93.  
  94. /* Nonzero means do not print commands to be executed (-s).  */
  95.  
  96. int silent_flag;
  97.  
  98. /* Nonzero means just touch the files
  99.    that would appear to need remaking (-t)  */
  100.  
  101. int touch_flag;
  102.  
  103. /* Nonzero means just print what commands would need to be executed,
  104.    don't actually execute them (-n).  */
  105.  
  106. int just_print_flag;
  107.  
  108. /* Print debugging trace info (-d).  */
  109.  
  110. int debug_flag = 0;
  111.  
  112. /* Environment variables override makefile definitions.  */
  113.  
  114. int env_overrides = 0;
  115.  
  116. /* Nonzero means ignore status codes returned by commands
  117.    executed to remake files.  Just treat them all as successful (-i).  */
  118.  
  119. int ignore_errors_flag = 0;
  120.  
  121. /* Nonzero means don't remake anything, just print the data base
  122.    that results from reading the makefile (-p).  */
  123.  
  124. int print_data_base_flag = 0;
  125.  
  126. /* Nonzero means don't remake anything; just return a nonzero status
  127.    if the specified targets are not up to date (-q).  */
  128.  
  129. int question_flag = 0;
  130.  
  131. /* Nonzero means do not use any of the builtin rules (-r).  */
  132.  
  133. int no_builtin_rules_flag = 0;
  134.  
  135. /* Nonzero means keep going even if remaking some file fails (-k).  */
  136.  
  137. int keep_going_flag;
  138. int default_keep_going_flag = 0;
  139.  
  140. /* Nonzero means print directory before starting and when done (-w).  */
  141.  
  142. int print_directory_flag = 0;
  143.  
  144. /* Nonzero means ignore print_directory_flag and never print the directory.
  145.    This is necessary because print_directory_flag is set implicitly.  */
  146.  
  147. int inhibit_print_directory_flag = 0;
  148.  
  149. /* Nonzero means print version information.  */
  150.  
  151. int print_version_flag = 0;
  152.  
  153. /* List of makefiles given with -f switches.  */
  154.  
  155. static struct stringlist *makefiles = 0;
  156.  
  157.  
  158. /* Number of job slots (commands that can be run at once).  */
  159.  
  160. unsigned int job_slots = 1;
  161. unsigned int default_job_slots = 1;
  162.  
  163. /* Value of job_slots that means no limit.  */
  164.  
  165. static unsigned int inf_jobs = 0;
  166.  
  167. /* Maximum load average at which multiple jobs will be run.
  168.    Negative values mean unlimited, while zero means limit to
  169.    zero load (which could be useful to start infinite jobs remotely
  170.    but one at a time locally).  */
  171.  
  172. double max_load_average = -1.0;
  173. double default_load_average = -1.0;
  174.  
  175. /* List of directories given with -C switches.  */
  176.  
  177. static struct stringlist *directories = 0;
  178.  
  179. /* List of include directories given with -I switches.  */
  180.  
  181. static struct stringlist *include_directories = 0;
  182.  
  183. /* List of files given with -o switches.  */
  184.  
  185. static struct stringlist *old_files = 0;
  186.  
  187. /* List of files given with -W switches.  */
  188.  
  189. static struct stringlist *new_files = 0;
  190.  
  191. /* If nonzero, we should just print usage and exit.  */
  192.  
  193. static int print_usage_flag = 0;
  194.  
  195. /* If nonzero, we should print a warning message
  196.    for each reference to an undefined variable.  */
  197.  
  198. int warn_undefined_variables_flag;
  199.  
  200. /* The table of command switches.  */
  201.  
  202. static const struct command_switch switches[] =
  203.   {
  204.     { 'b', ignore, 0, 0, 0, 0, 0, 0,
  205.     0, 0,
  206.     "Ignored for compatibility" },
  207.     { 'C', string, (char *) &directories, 0, 0, 0, 0, 0,
  208.     "directory", "DIRECTORY",
  209.     "Change to DIRECTORY before doing anything" },
  210.     { 'd', flag, (char *) &debug_flag, 1, 1, 0, 0, 0,
  211.     "debug", 0,
  212.     "Print lots of debugging information" },
  213.     { 'e', flag, (char *) &env_overrides, 1, 1, 0, 0, 0,
  214.     "environment-overrides", 0,
  215.     "Environment variables override makefiles" },
  216.     { 'f', string, (char *) &makefiles, 0, 0, 0, 0, 0,
  217.     "file", "FILE",
  218.     "Read FILE as a makefile" },
  219.     { 'h', flag, (char *) &print_usage_flag, 0, 0, 0, 0, 0,
  220.     "help", 0,
  221.     "Print this message and exit" },
  222.     { 'i', flag, (char *) &ignore_errors_flag, 1, 1, 0, 0, 0,
  223.     "ignore-errors", 0,
  224.     "Ignore errors from commands" },
  225.     { 'I', string, (char *) &include_directories, 1, 1, 0, 0, 0,
  226.     "include-dir", "DIRECTORY",
  227.     "Search DIRECTORY for included makefiles" },
  228.     { 'j', positive_int, (char *) &job_slots, 1, 1, 0,
  229.     (char *) &inf_jobs, (char *) &default_job_slots,
  230.     "jobs", "N",
  231.     "Allow N jobs at once; infinite jobs with no arg" },
  232.     { 'k', flag, (char *) &keep_going_flag, 1, 1, 0,
  233.     0, (char *) &default_keep_going_flag,
  234.     "keep-going", 0,
  235.     "Keep going when some targets can't be made" },
  236.     { 'l', floating, (char *) &max_load_average, 1, 1, 0,
  237.     (char *) &default_load_average, (char *) &default_load_average,
  238.     "load-average", "N",
  239.     "Don't start multiple jobs unless load is below N" },
  240.     { 'm', ignore, 0, 0, 0, 0, 0, 0,
  241.     0, 0,
  242.     "-b" },
  243.     { 'n', flag, (char *) &just_print_flag, 1, 1, 1, 0, 0,
  244.     "just-print", 0,
  245.     "Don't actually run any commands; just print them" },
  246.     { 'o', string, (char *) &old_files, 0, 0, 0, 0, 0,
  247.     "old-file", "FILE",
  248.     "Consider FILE to be very old and don't remake it" },
  249.     { 'p', flag, (char *) &print_data_base_flag, 1, 1, 0, 0, 0,
  250.     "print-data-base", 0,
  251.     "Print make's internal database" },
  252.     { 'q', flag, (char *) &question_flag, 1, 1, 1, 0, 0,
  253.     "question", 0,
  254.     "Run no commands; exit status says if up to date" },
  255.     { 'r', flag, (char *) &no_builtin_rules_flag, 1, 1, 0, 0, 0,
  256.     "no-builtin-rules", 0,
  257.     "Disable the built-in implicit rules" },
  258.     { 's', flag, (char *) &silent_flag, 1, 1, 0, 0, 0,
  259.     "silent", 0,
  260.     "Don't echo commands" },
  261.     { 'S', flag_off, (char *) &keep_going_flag, 1, 1, 0,
  262.     0, (char *) &default_keep_going_flag,
  263.     "no-keep-going", 0,
  264.     "Turns off -k" },
  265.     { 't', flag, (char *) &touch_flag, 1, 1, 1, 0, 0,
  266.     "touch", 0,
  267.     "Touch targets instead of remaking them" },
  268.     { 'v', flag, (char *) &print_version_flag, 1, 1, 0, 0, 0,
  269.     "version", 0,
  270.     "Print the version number of make and exit" },
  271.     { 'w', flag, (char *) &print_directory_flag, 1, 1, 0, 0, 0,
  272.     "print-directory", 0,
  273.     "Print the current directory" },
  274.     { 1, flag, (char *) &inhibit_print_directory_flag, 1, 1, 0, 0, 0,
  275.     "no-print-directory", 0,
  276.     "Turn off -w, even if it was turned on implicitly" },
  277.     { 'W', string, (char *) &new_files, 0, 0, 0, 0, 0,
  278.     "what-if", "FILE",
  279.     "Consider FILE to be infinitely new" },
  280.     { 2, flag, (char *) &warn_undefined_variables_flag, 1, 1, 0, 0, 0,
  281.     "warn-undefined-variables", 0,
  282.     "Warn when an undefined variable is referenced" },
  283.     { '\0', }
  284.   };
  285.  
  286. /* Secondary long names for options.  */
  287.  
  288. static struct option long_option_aliases[] =
  289.   {
  290.     { "quiet",        no_argument,        0, 's' },
  291.     { "stop",        no_argument,        0, 'S' },
  292.     { "new-file",    required_argument,    0, 'W' },
  293.     { "assume-new",    required_argument,    0, 'W' },
  294.     { "assume-old",    required_argument,    0, 'o' },
  295.     { "max-load",    optional_argument,    0, 'l' },
  296.     { "dry-run",    no_argument,        0, 'n' },
  297.     { "recon",        no_argument,        0, 'n' },
  298.     { "makefile",    required_argument,    0, 'f' },
  299.   };
  300.  
  301. /* The usage message prints the descriptions of options starting in
  302.    this column.  Make sure it leaves enough room for the longest
  303.    description to fit in less than 80 characters.  */
  304.  
  305. #define    DESCRIPTION_COLUMN    30
  306.  
  307. /* List of non-switch arguments.  */
  308.  
  309. struct stringlist *other_args = 0;
  310.  
  311. /* The name we were invoked with.  */
  312.  
  313. char *program;
  314.  
  315. /* Our current directory after processing all -C options.  */
  316.  
  317. char *starting_directory;
  318.  
  319. /* Value of the MAKELEVEL variable at startup (or 0).  */
  320.  
  321. unsigned int makelevel;
  322.  
  323. /* First file defined in the makefile whose name does not
  324.    start with `.'.  This is the default to remake if the
  325.    command line does not specify.  */
  326.  
  327. struct file *default_goal_file;
  328.  
  329. /* Pointer to structure for the file .DEFAULT
  330.    whose commands are used for any file that has none of its own.
  331.    This is zero if the makefiles do not define .DEFAULT.  */
  332.  
  333. struct file *default_file;
  334.  
  335. /* Mask of signals that are being caught with fatal_error_signal.  */
  336.  
  337. #ifdef    POSIX
  338. sigset_t fatal_signal_set;
  339. #else
  340. #ifdef    HAVE_SIGSETMASK
  341. int fatal_signal_mask;
  342. #endif
  343. #endif
  344.  
  345. static struct file *
  346. enter_command_line_file (name)
  347.      char *name;
  348. {
  349.   if (name[0] == '~')
  350.     {
  351.       char *expanded = tilde_expand (name);
  352.       if (expanded != 0)
  353.     name = expanded;    /* Memory leak; I don't care.  */
  354.     }
  355.  
  356.   /* This is also done in parse_file_seq, so this is redundant
  357.      for names read from makefiles.  It is here for names passed
  358.      on the command line.  */
  359.   while (name[0] == '.' && name[1] == '/' && name[2] != '\0')
  360.     {
  361.       name += 2;
  362.       while (*name == '/')
  363.     /* Skip following slashes: ".//foo" is "foo", not "/foo".  */
  364.     ++name;
  365.     }
  366.   
  367.   if (*name == '\0')
  368.     {
  369.       /* It was all slashes!  Move back to the dot and truncate
  370.      it after the first slash, so it becomes just "./".  */
  371.       do
  372.     --name;
  373.       while (name[0] != '.');
  374.       name[2] = '\0';
  375.     }
  376.  
  377.   return enter_file (savestring (name, strlen (name)));
  378. }
  379.  
  380. int
  381. main (argc, argv, envp)
  382.      int argc;
  383.      char **argv;
  384.      char **envp;
  385. {
  386.   extern void init_dir ();
  387.   extern RETSIGTYPE fatal_error_signal (), child_handler ();
  388.   register struct file *f;
  389.   register unsigned int i;
  390.   register char *cmd_defs;
  391.   register unsigned int cmd_defs_len, cmd_defs_idx;
  392.   char **p;
  393.   struct dep *goals = 0;
  394.   register struct dep *lastgoal;
  395.   struct dep *read_makefiles;
  396.   PATH_VAR (current_directory);
  397.   char *directory_before_chdir;
  398.  
  399. #ifdef __EMX__
  400.   _response(&argc,&argv);
  401.   _wildcard(&argc,&argv);
  402. #endif
  403.  
  404.   default_goal_file = 0;
  405.   reading_filename = 0;
  406.   reading_lineno_ptr = 0;
  407.   
  408. #ifndef    HAVE_SYS_SIGLIST
  409.   signame_init ();
  410. #endif
  411.  
  412. #ifdef    POSIX
  413.   sigemptyset (&fatal_signal_set);
  414. #define    ADD_SIG(sig)    sigaddset (&fatal_signal_set, sig)
  415. #else
  416. #ifdef    HAVE_SIGSETMASK
  417.   fatal_signal_mask = 0;
  418. #define    ADD_SIG(sig)    fatal_signal_mask |= sigmask (sig)
  419. #else
  420. #define    ADD_SIG(sig)
  421. #endif
  422. #endif
  423.  
  424. #define    FATAL_SIG(sig)                                  \
  425.   if (signal ((sig), fatal_error_signal) == SIG_IGN)                  \
  426.     (void) signal ((sig), SIG_IGN);                          \
  427.   else                                          \
  428.     ADD_SIG (sig);
  429.  
  430.   FATAL_SIG (SIGHUP);
  431.   FATAL_SIG (SIGQUIT);
  432.   FATAL_SIG (SIGINT);
  433.   FATAL_SIG (SIGTERM);
  434.  
  435. #ifdef    SIGDANGER
  436.   FATAL_SIG (SIGDANGER);
  437. #endif
  438. #ifdef SIGXCPU
  439.   FATAL_SIG (SIGXCPU);
  440. #endif
  441. #ifdef SIGXFSZ
  442.   FATAL_SIG (SIGXFSZ);
  443. #endif
  444.  
  445. #undef    FATAL_SIG
  446.  
  447.   /* Make sure stdout is line-buffered.  */
  448.  
  449. #ifdef    HAVE_SETLINEBUF
  450.   setlinebuf (stdout);
  451. #else
  452. #ifndef    SETVBUF_REVERSED
  453.   setvbuf (stdout, (char *) 0, _IOLBF, BUFSIZ);
  454. #else    /* setvbuf not reversed.  */
  455.   /* Some buggy systems lose if we pass 0 instead of allocating ourselves.  */
  456.   setvbuf (stdout, _IOLBF, xmalloc (BUFSIZ), BUFSIZ);
  457. #endif    /* setvbuf reversed.  */
  458. #endif    /* setlinebuf missing.  */
  459.  
  460.   /* Initialize the directory hashing code.  */
  461.   init_dir ();
  462.  
  463.   /* Figure out where this program lives.  */
  464.  
  465.   if (argv[0] == 0)
  466.     argv[0] = "";
  467.   if (argv[0][0] == '\0')
  468.     program = "make";
  469.   else 
  470.     {
  471.       program = rindex (argv[0], '/');
  472.       if (program == 0)
  473.     program = argv[0];
  474.       else
  475.     ++program;
  476.     }
  477.  
  478.   /* Set up to access user data (files).  */
  479.   user_access ();
  480.  
  481.   /* Figure out where we are.  */
  482.  
  483.   if (getcwd (current_directory, GET_PATH_MAX) == 0)
  484.     {
  485. #ifdef    HAVE_GETCWD
  486.       perror_with_name ("getcwd: ", "");
  487. #else
  488.       error ("getwd: %s", current_directory);
  489. #endif
  490.       current_directory[0] = '\0';
  491.       directory_before_chdir = 0;
  492.     }
  493.   else
  494.     directory_before_chdir = savestring (current_directory,
  495.                      strlen (current_directory));
  496.  
  497.   /* Read in variables from the environment.  It is important that this be
  498.      done before `MAKE' and `MAKEOVERRIDES' are figured out so their
  499.      definitions will not be ones from the environment.  */
  500.  
  501.   for (i = 0; envp[i] != 0; ++i)
  502.     {
  503.       register char *ep = envp[i];
  504.       while (*ep != '=')
  505.     ++ep;
  506.       /* The result of pointer arithmetic is cast to unsigned int for
  507.      machines where ptrdiff_t is a different size that doesn't widen
  508.      the same.  */
  509.       define_variable (envp[i], (unsigned int) (ep - envp[i]),
  510.                ep + 1, o_env, 1)
  511.     /* Force exportation of every variable culled from the environment.
  512.        We used to rely on target_environment's v_default code to do this.
  513.        But that does not work for the case where an environment variable
  514.        is redefined in a makefile with `override'; it should then still
  515.        be exported, because it was originally in the environment.  */
  516.     ->export = v_export;
  517.     }
  518.  
  519.   /* Decode the switches.  */
  520.  
  521.   decode_env_switches ("MAKEFLAGS", 9);
  522. #if 0
  523.   /* People write things like:
  524.          MFLAGS="CC=gcc -pipe" "CFLAGS=-g"
  525.      and we set the -p, -i and -e switches.  Doesn't seem quite right.  */
  526.   decode_env_switches ("MFLAGS", 6);
  527. #endif
  528.   decode_switches (argc, argv, 0);
  529.  
  530.   /* Print version information.  */
  531.  
  532.   if (print_version_flag || print_data_base_flag || debug_flag)
  533.     print_version ();
  534.  
  535.   /* `make --version' is supposed to just print the version and exit.  */
  536.   if (print_version_flag)
  537.     die (0);
  538.  
  539.   /* Search for command line arguments that define variables,
  540.      and do the definitions.  Also save up the text of these
  541.      arguments in CMD_DEFS so we can put them into the values
  542.      of $(MAKEOVERRIDES) and $(MAKE).  */
  543.  
  544.   cmd_defs_len = 200;
  545.   cmd_defs = (char *) xmalloc (cmd_defs_len);
  546.   cmd_defs_idx = 0;
  547.  
  548.   for (i = 1; other_args->list[i] != 0; ++i)
  549.     {
  550.       if (other_args->list[i][0] == '\0')
  551.     /* Ignore empty arguments, so they can't kill enter_file.  */
  552.     continue;
  553.  
  554.       /* Try a variable definition.  */
  555.       if (try_variable_definition ((char *) 0, 0,
  556.                    other_args->list[i], o_command))
  557.     {
  558.       /* It succeeded.  The variable is already defined.
  559.          Backslash-quotify it and append it to CMD_DEFS, then clobber it
  560.          to 0 in the list so that it won't be taken for a goal target.  */
  561.       register char *p = other_args->list[i];
  562.       unsigned int l = strlen (p);
  563.       if (cmd_defs_idx + (l * 2) + 1 > cmd_defs_len)
  564.         {
  565.           if (l * 2 > cmd_defs_len)
  566.         cmd_defs_len += l * 2;
  567.           else
  568.         cmd_defs_len *= 2;
  569.           cmd_defs = (char *) xrealloc (cmd_defs, cmd_defs_len);
  570.         }
  571.       
  572.       while (*p != '\0')
  573.         {
  574.           if (index ("^;'\"*?[]$<>(){}|&~`\\ \t\r\n\f\v", *p) != 0)
  575.         cmd_defs[cmd_defs_idx++] = '\\';
  576.           cmd_defs[cmd_defs_idx++] = *p++;
  577.         }
  578.       cmd_defs[cmd_defs_idx++] = ' ';
  579.     }
  580.       else
  581.     {
  582.       /* It was not a variable definition, so it is a target to be made.
  583.          Enter it as a file and add it to the dep chain of goals.  */
  584.       f = enter_command_line_file (other_args->list[i]);
  585.       f->cmd_target = 1;
  586.       
  587.       if (goals == 0)
  588.         {
  589.           goals = (struct dep *) xmalloc (sizeof (struct dep));
  590.           lastgoal = goals;
  591.         }
  592.       else
  593.         {
  594.           lastgoal->next = (struct dep *) xmalloc (sizeof (struct dep));
  595.           lastgoal = lastgoal->next;
  596.         }
  597.       lastgoal->name = 0;
  598.       lastgoal->file = f;
  599.     }
  600.     }
  601.  
  602.   if (cmd_defs_idx > 0)
  603.     {
  604.       cmd_defs[cmd_defs_idx - 1] = '\0';
  605.       (void) define_variable ("MAKEOVERRIDES", 13, cmd_defs, o_default, 0);
  606.     }
  607.   free (cmd_defs);
  608.  
  609.   /* Set the "MAKE_COMMAND" variable to the name we were invoked with.
  610.      (If it is a relative pathname with a slash, prepend our directory name
  611.      so the result will run the same program regardless of the current dir.
  612.      If it is a name with no slash, we can only hope that PATH did not
  613.      find it in the current directory.)  */
  614.  
  615. #ifndef __EMX__
  616.   if (current_directory[0] != '\0'
  617.       && argv[0] != 0 && argv[0][0] != '/' && index (argv[0], '/') != 0)
  618.     argv[0] = concat (current_directory, "/", argv[0]);
  619. #else
  620.   if (current_directory[0] != '\0' && /* there is a cwd */
  621.       argv[0] != 0 &&            /* there is an argv[0] */
  622.       (argv[0][0] != '/' &&        /* and it doesn't begin with slash */
  623.       argv[0][0] != '\\' &&        /* or a backslash */
  624.       argv[0][1] != ':') &&        /* or start with a drive letter */
  625.       (index (argv[0], '/') != 0 ||    /*  it does contain a slash */
  626.       index (argv[0], '\\') != 0))    /* or a backslash */
  627.     argv[0] = concat (current_directory, "/", argv[0]);
  628.   {
  629.     char *cp;
  630.     for(cp = argv[0]; *cp != '\0'; cp++)
  631.       *cp = (*cp == '\\') ? '/' : *cp;
  632.   }
  633. #endif
  634.   (void) define_variable ("MAKE_COMMAND", 12, argv[0], o_default, 0);
  635.  
  636.   /* Append the command-line variable definitions gathered above
  637.      so sub-makes will get them as command-line definitions.  */
  638.  
  639.   (void) define_variable ("MAKE", 4,
  640.               "$(MAKE_COMMAND) $(MAKEOVERRIDES)", o_default, 1);
  641.  
  642.   /* If there were -C flags, move ourselves about.  */
  643.  
  644.   if (directories != 0)
  645.     for (i = 0; directories->list[i] != 0; ++i)
  646.       {
  647.     char *dir = directories->list[i];
  648.     if (dir[0] == '~')
  649.       {
  650.         char *expanded = tilde_expand (dir);
  651.         if (expanded != 0)
  652.           dir = expanded;
  653.       }
  654.     if (chdir (dir) < 0)
  655.       pfatal_with_name (dir);
  656.     if (dir != directories->list[i])
  657.       free (dir);
  658.       }
  659.  
  660.   /* Figure out the level of recursion.  */
  661.   {
  662.     struct variable *v = lookup_variable ("MAKELEVEL", 9);
  663.     if (v != 0 && *v->value != '\0' && *v->value != '-')
  664.       makelevel = (unsigned int) atoi (v->value);
  665.     else
  666.       makelevel = 0;
  667.   }
  668.  
  669.   /* Except under -s, always do -w in sub-makes and under -C.  */
  670.   if (!silent_flag && (directories != 0 || makelevel > 0))
  671.     print_directory_flag = 1;
  672.  
  673.   /* Let the user disable that with --no-print-directory.  */
  674.   if (inhibit_print_directory_flag)
  675.     print_directory_flag = 0;
  676.  
  677.   /* Construct the list of include directories to search.  */
  678.  
  679.   construct_include_path (include_directories == 0 ? (char **) 0
  680.               : include_directories->list);
  681.  
  682.   /* Figure out where we are now, after chdir'ing.  */
  683.   if (directories == 0)
  684.     /* We didn't move, so we're still in the same place.  */
  685.     starting_directory = current_directory;
  686.   else
  687.     {
  688.       if (getcwd (current_directory, GET_PATH_MAX) == 0)
  689.     {
  690. #ifdef    HAVE_GETCWD
  691.       perror_with_name ("getcwd: ", "");
  692. #else
  693.       error ("getwd: %s", current_directory);
  694. #endif
  695.       starting_directory = 0;
  696.     }
  697.       else
  698.     starting_directory = current_directory;
  699.     }
  700.  
  701.   /* Tell the user where he is.  */
  702.  
  703.   if (print_directory_flag)
  704.     log_working_directory (1);
  705.  
  706.   /* Read any stdin makefiles into temporary files.  */
  707.  
  708.   if (makefiles != 0)
  709.     {
  710.       register unsigned int i;
  711.       for (i = 0; i < makefiles->idx; ++i)
  712.     if (makefiles->list[i][0] == '-' && makefiles->list[i][1] == '\0')
  713.       {
  714.         /* This makefile is standard input.  Since we may re-exec
  715.            and thus re-read the makefiles, we read standard input
  716.            into a temporary file and read from that.  */
  717.         char name[PATH_MAX];
  718.         FILE *outfile;
  719.  
  720.         /* Make a unique filename.  */
  721.         tmpnam (name);
  722.  
  723.         outfile = fopen (name, "w");
  724.         if (outfile == 0)
  725.           pfatal_with_name ("fopen (temporary file)");
  726.         while (!feof (stdin))
  727.           {
  728.         char buf[2048];
  729.         int n = fread (buf, 1, sizeof(buf), stdin);
  730.         if (n > 0 && fwrite (buf, 1, n, outfile) != n)
  731.           pfatal_with_name ("fwrite (temporary file)");
  732.           }
  733.         /* Try to make sure we won't remake the temporary
  734.            file when we are re-exec'd.  Kludge-o-matic!  */
  735.         fprintf (outfile, "%s:;\n", name);
  736.         (void) fclose (outfile);
  737.  
  738.         /* Replace the name that read_all_makefiles will
  739.            see with the name of the temporary file.  */
  740.         {
  741.           char *temp;
  742.           /* SGI compiler requires alloca's result be assigned simply.  */
  743.           temp = (char *) alloca (sizeof (name));
  744.           bcopy (name, temp, sizeof (name));
  745.           makefiles->list[i] = temp;
  746.         }
  747.  
  748.         /* Make sure the temporary file will not be remade.  */
  749.         f = enter_file (savestring (name, sizeof name - 1));
  750.         f->updated = 1;
  751.         f->update_status = 0;
  752.         f->command_state = cs_finished;
  753.         /* Let it be removed when we're done.  */
  754.         f->intermediate = 1;
  755.         /* But don't mention it.  */
  756.         f->dontcare = 1;
  757.       }
  758.     }
  759.  
  760.   /* Set up to handle children dying.  This must be done before
  761.      reading in the makefiles so that `shell' function calls will work.  */
  762.  
  763. #ifdef SIGCHLD
  764.   (void) signal (SIGCHLD, child_handler);
  765. #endif
  766. #ifdef SIGCLD
  767.   (void) signal (SIGCLD, child_handler);
  768. #endif
  769.  
  770.   /* Define the initial list of suffixes for old-style rules.  */
  771.  
  772.   set_default_suffixes ();
  773.  
  774.   /* Define the file rules for the built-in suffix rules.  These will later
  775.      be converted into pattern rules.  We used to do this in
  776.      install_default_implicit_rules, but since that happens after reading
  777.      makefiles, it results in the built-in pattern rules taking precedence
  778.      over makefile-specified suffix rules, which is wrong.  */
  779.  
  780.   install_default_suffix_rules ();
  781.  
  782.   /* Define some internal and special variables.  */
  783.  
  784.   define_automatic_variables ();
  785.  
  786.   /* Set up the MAKEFLAGS and MFLAGS variables
  787.      so makefiles can look at them.  */
  788.  
  789.   define_makeflags (0, 0);
  790.  
  791.   /* Define the default variables.  */
  792.   define_default_variables ();
  793.  
  794.   /* Read all the makefiles.  */
  795.  
  796.   default_file = enter_file (".DEFAULT");
  797.  
  798.   read_makefiles
  799.     = read_all_makefiles (makefiles == 0 ? (char **) 0 : makefiles->list);
  800.  
  801.   /* Decode switches again, in case the variables were set by the makefile.  */
  802.   decode_env_switches ("MAKEFLAGS", 9);
  803. #if 0
  804.   decode_env_switches ("MFLAGS", 6);
  805. #endif
  806.  
  807.   /* Set up MAKEFLAGS and MFLAGS again, so they will be right.  */
  808.  
  809.   define_makeflags (1, 0);
  810.  
  811.   ignore_errors_flag |= lookup_file (".IGNORE") != 0;
  812.  
  813.   silent_flag |= lookup_file (".SILENT") != 0;
  814.  
  815.   /* Make each `struct dep' point at the
  816.      `struct file' for the file depended on.  */
  817.  
  818.   snap_deps ();
  819.  
  820.   /* Convert old-style suffix rules to pattern rules.  It is important to
  821.      do this before installing the built-in pattern rules below, so that
  822.      makefile-specified suffix rules take precedence over built-in pattern
  823.      rules.  */
  824.  
  825.   convert_to_pattern ();
  826.  
  827.   /* Install the default implicit pattern rules.
  828.      This used to be done before reading the makefiles.
  829.      But in that case, built-in pattern rules were in the chain
  830.      before user-defined ones, so they matched first.  */
  831.  
  832.   install_default_implicit_rules ();
  833.  
  834.   /* Compute implicit rule limits.  */
  835.  
  836.   count_implicit_rule_limits ();
  837.  
  838.   /* Construct the listings of directories in VPATH lists.  */
  839.  
  840.   build_vpath_lists ();
  841.  
  842.   /* Mark files given with -o flags as very old (00:00:01.00 Jan 1, 1970)
  843.      and as having been updated already, and files given with -W flags as
  844.      brand new (time-stamp as far as possible into the future).  */
  845.  
  846.   if (old_files != 0)
  847.     for (p = old_files->list; *p != 0; ++p)
  848.       {
  849.     f = enter_command_line_file (*p);
  850.     f->last_mtime = (time_t) 1;
  851.     f->updated = 1;
  852.     f->update_status = 0;
  853.     f->command_state = cs_finished;
  854.       }
  855.  
  856.   if (new_files != 0)
  857.     {
  858.       for (p = new_files->list; *p != 0; ++p)
  859.     {
  860.       f = enter_command_line_file (*p);
  861.       f->last_mtime = NEW_MTIME;
  862.     }
  863.     }
  864.  
  865.   if (read_makefiles != 0)
  866.     {
  867.       /* Update any makefiles if necessary.  */
  868.  
  869.       time_t *makefile_mtimes = 0;
  870.       unsigned int mm_idx = 0;
  871.  
  872.       if (debug_flag)
  873.     puts ("Updating makefiles....");
  874.  
  875.       /* Remove any makefiles we don't want to try to update.
  876.      Also record the current modtimes so we can compare them later.  */
  877.       {
  878.     register struct dep *d, *last;
  879.     last = 0;
  880.     d = read_makefiles;
  881.     while (d != 0)
  882.       {
  883.         register struct file *f = d->file;
  884.         if (f->double_colon)
  885.           for (f = f->double_colon; f != NULL; f = f->prev)
  886.         {
  887.           if (f->deps == 0 && f->cmds != 0)
  888.             {
  889.               /* This makefile is a :: target with commands, but
  890.              no dependencies.  So, it will always be remade.
  891.              This might well cause an infinite loop, so don't
  892.              try to remake it.  (This will only happen if
  893.              your makefiles are written exceptionally
  894.              stupidly; but if you work for Athena, that's how
  895.              you write your makefiles.)  */
  896.  
  897.               if (debug_flag)
  898.             printf ("Makefile `%s' might loop; not remaking it.\n",
  899.                 f->name);
  900.  
  901.               if (last == 0)
  902.             read_makefiles = d->next;
  903.               else
  904.             last->next = d->next;
  905.  
  906.               /* Free the storage.  */
  907.               free ((char *) d);
  908.  
  909.               d = last == 0 ? 0 : last->next;
  910.  
  911.               break;
  912.             }
  913.         }
  914.         if (f == NULL || !f->double_colon)
  915.           {
  916.         if (makefile_mtimes == 0)
  917.           makefile_mtimes = (time_t *) xmalloc (sizeof (time_t));
  918.         else
  919.           makefile_mtimes = (time_t *)
  920.             xrealloc ((char *) makefile_mtimes,
  921.                   (mm_idx + 1) * sizeof (time_t));
  922.         makefile_mtimes[mm_idx++] = file_mtime_no_search (d->file);
  923.         last = d;
  924.         d = d->next;
  925.           }
  926.       }
  927.       }    
  928.  
  929.       /* Set up `MAKEFLAGS' specially while remaking makefiles.  */
  930.       define_makeflags (1, 1);
  931.  
  932.       switch (update_goal_chain (read_makefiles, 1))
  933.     {
  934.     default:
  935.       abort ();
  936.       
  937.     case -1:
  938.       /* Did nothing.  */
  939.       break;
  940.       
  941.     case 1:
  942.       /* Failed to update.  Figure out if we care.  */
  943.       {
  944.         /* Nonzero if any makefile was successfully remade.  */
  945.         int any_remade = 0;
  946.         /* Nonzero if any makefile we care about failed
  947.            in updating or could not be found at all.  */
  948.         int any_failed = 0;
  949.         register unsigned int i;
  950.  
  951.         for (i = 0; read_makefiles != 0; ++i)
  952.           {
  953.         struct dep *d = read_makefiles;
  954.         read_makefiles = d->next;
  955.         if (d->file->updated)
  956.           {
  957.             /* This makefile was updated.  */
  958.             if (d->file->update_status == 0)
  959.               {
  960.             /* It was successfully updated.  */
  961.             any_remade |= (file_mtime_no_search (d->file)
  962.                        != makefile_mtimes[i]);
  963.               }
  964.             else if (! (d->changed & RM_DONTCARE))
  965.               {
  966.             time_t mtime;
  967.             /* The update failed and this makefile was not
  968.                from the MAKEFILES variable, so we care.  */
  969.             error ("Failed to remake makefile `%s'.",
  970.                    d->file->name);
  971.             mtime = file_mtime_no_search (d->file);
  972.             any_remade |= (mtime != (time_t) -1
  973.                        && mtime != makefile_mtimes[i]);
  974.               }
  975.           }
  976.         else
  977.           /* This makefile was not found at all.  */
  978.           if (! (d->changed & RM_DONTCARE))
  979.             {
  980.               /* This is a makefile we care about.  See how much.  */
  981.               if (d->changed & RM_INCLUDED)
  982.             /* An included makefile.  We don't need
  983.                to die, but we do want to complain.  */
  984.             error ("Included makefile `%s' was not found.",
  985.                    dep_name (d));
  986.               else
  987.             {
  988.               /* A normal makefile.  We must die later.  */
  989.               error ("Makefile `%s' was not found", dep_name (d));
  990.               any_failed = 1;
  991.             }
  992.             }
  993.  
  994.         free ((char *) d);
  995.           }
  996.  
  997.         if (any_remade)
  998.           goto re_exec;
  999.         else if (any_failed)
  1000.           die (2);
  1001.         else
  1002.           break;
  1003.       }
  1004.  
  1005.     case 0:
  1006.     re_exec:
  1007.       /* Updated successfully.  Re-exec ourselves.  */
  1008.  
  1009.       remove_intermediates (0);
  1010.  
  1011.       if (print_data_base_flag)
  1012.         print_data_base ();
  1013.  
  1014.       if (print_directory_flag)
  1015.         log_working_directory (0);
  1016.  
  1017.       if (makefiles != 0)
  1018.         {
  1019.           /* These names might have changed.  */
  1020.           register unsigned int i, j = 0;
  1021.           for (i = 1; i < argc; ++i)
  1022.         if (!strcmp (argv[i], "-f")) /* XXX */
  1023.           {
  1024.             char *p = &argv[i][2];
  1025.             if (*p == '\0')
  1026.               argv[++i] = makefiles->list[j];
  1027.             else
  1028.               argv[i] = concat ("-f", makefiles->list[j], "");
  1029.             ++j;
  1030.           }
  1031.         }
  1032.  
  1033.       if (directories != 0 && directories->idx > 0)
  1034.         {
  1035.           char bad;
  1036.           if (directory_before_chdir != 0)
  1037.         {
  1038.           if (chdir (directory_before_chdir) < 0)
  1039.             {
  1040.               perror_with_name ("chdir", "");
  1041.               bad = 1;
  1042.             }
  1043.           else
  1044.             bad = 0;
  1045.         }
  1046.           else
  1047.         bad = 1;
  1048.           if (bad)
  1049.         fatal ("Couldn't change back to original directory.");
  1050.         }
  1051.  
  1052.       for (p = environ; *p != 0; ++p)
  1053.         if (!strncmp (*p, "MAKELEVEL=", 10))
  1054.           {
  1055.         /* The SGI compiler apparently can't understand
  1056.            the concept of storing the result of a function
  1057.            in something other than a local variable.  */
  1058.         char *sgi_loses;
  1059.         sgi_loses = (char *) alloca (40);
  1060.         *p = sgi_loses;
  1061.         sprintf (*p, "MAKELEVEL=%u", makelevel);
  1062.         break;
  1063.           }
  1064.  
  1065.       if (debug_flag)
  1066.         {
  1067.           char **p;
  1068.           fputs ("Re-executing:", stdout);
  1069.           for (p = argv; *p != 0; ++p)
  1070.         printf (" %s", *p);
  1071.           puts ("");
  1072.         }
  1073.  
  1074.       fflush (stdout);
  1075.       fflush (stderr);
  1076.  
  1077. #ifndef __EMX__
  1078.       exec_command (argv, environ);
  1079. #else
  1080.       exit(exec_command (argv, environ));
  1081. #endif
  1082.       /* NOTREACHED */
  1083.     }
  1084.     }
  1085.  
  1086.   /* Set up `MAKEFLAGS' again for the normal targets.  */
  1087.   define_makeflags (1, 0);
  1088.  
  1089.   {
  1090.     int status;
  1091.  
  1092.     /* If there were no command-line goals, use the default.  */
  1093.     if (goals == 0)
  1094.       {
  1095.     if (default_goal_file != 0)
  1096.       {
  1097.         goals = (struct dep *) xmalloc (sizeof (struct dep));
  1098.         goals->next = 0;
  1099.         goals->name = 0;
  1100.         goals->file = default_goal_file;
  1101.       }
  1102.       }
  1103.     else
  1104.       lastgoal->next = 0;
  1105.  
  1106.     if (goals != 0)
  1107.       {
  1108.     /* Update the goals.  */
  1109.  
  1110.     if (debug_flag)
  1111.       puts ("Updating goal targets....");
  1112.  
  1113.     switch (update_goal_chain (goals, 0))
  1114.       {
  1115.       case -1:
  1116.         /* Nothing happened.  */
  1117.       case 0:
  1118.         /* Updated successfully.  */
  1119.         status = 0;
  1120.         break;
  1121.       case 2:
  1122.         /* Updating failed.  */
  1123.         status = 2;
  1124.         break;
  1125.       case 1:
  1126.         /* We are under -q and would run some commands.  */
  1127.         status = 1;
  1128.         break;
  1129.       default:
  1130.         abort ();
  1131.       }
  1132.       }
  1133.     else
  1134.       {
  1135.     if (read_makefiles == 0)
  1136.       fatal ("No targets specified and no makefile found");
  1137.     else
  1138.       fatal ("No targets");
  1139.       }
  1140.  
  1141.     /* Exit.  */
  1142.     die (status);
  1143.   }
  1144.  
  1145.   return 0;
  1146. }
  1147.  
  1148. /* Parsing of arguments, decoding of switches.  */
  1149.  
  1150. static char options[sizeof (switches) / sizeof (switches[0]) * 3];
  1151. static struct option long_options[(sizeof (switches) / sizeof (switches[0])) +
  1152.                   (sizeof (long_option_aliases) /
  1153.                    sizeof (long_option_aliases[0]))];
  1154.  
  1155. /* Fill in the string and vector for getopt.  */
  1156. static void
  1157. init_switches ()
  1158. {
  1159.   register char *p;
  1160.   register int c;
  1161.   register unsigned int i;
  1162.  
  1163.   if (options[0] != '\0')
  1164.     /* Already done.  */
  1165.     return;
  1166.  
  1167.   p = options;
  1168.   for (i = 0; switches[i].c != '\0'; ++i)
  1169.     {
  1170.       long_options[i].name = (switches[i].long_name == 0 ? "" :
  1171.                   switches[i].long_name);
  1172.       long_options[i].flag = 0;
  1173.       long_options[i].val = switches[i].c;
  1174.       if (isalnum (switches[i].c))
  1175.     *p++ = switches[i].c;
  1176.       switch (switches[i].type)
  1177.     {
  1178.     case flag:
  1179.     case flag_off:
  1180.     case ignore:
  1181.       long_options[i].has_arg = no_argument;
  1182.       break;
  1183.  
  1184.     case string:
  1185.     case positive_int:
  1186.     case floating:
  1187.       if (isalnum (switches[i].c))
  1188.         *p++ = ':';
  1189.       if (switches[i].noarg_value != 0)
  1190.         {
  1191.           if (isalnum (switches[i].c))
  1192.         *p++ = ':';
  1193.           long_options[i].has_arg = optional_argument;
  1194.         }
  1195.       else
  1196.         long_options[i].has_arg = required_argument;
  1197.       break;
  1198.     }
  1199.     }
  1200.   *p = '\0';
  1201.   for (c = 0; c < (sizeof (long_option_aliases) /
  1202.            sizeof (long_option_aliases[0]));
  1203.        ++c)
  1204.     long_options[i++] = long_option_aliases[c];
  1205.   long_options[i].name = 0;
  1206. }
  1207.  
  1208. /* Decode switches from ARGC and ARGV.
  1209.    They came from the environment if ENV is nonzero.  */
  1210.  
  1211. static void
  1212. decode_switches (argc, argv, env)
  1213.      int argc;
  1214.      char **argv;
  1215.      int env;
  1216. {
  1217.   int bad = 0;
  1218.   register const struct command_switch *cs;
  1219.   register struct stringlist *sl;
  1220.   register int c;
  1221.  
  1222.   if (!env)
  1223.     {
  1224.       other_args = (struct stringlist *) xmalloc (sizeof (struct stringlist));
  1225.       other_args->max = argc + 1;
  1226.       other_args->list = (char **) xmalloc ((argc + 1) * sizeof (char *));
  1227.       other_args->idx = 1;
  1228.       other_args->list[0] = argv[0];
  1229.     }
  1230.  
  1231.   /* getopt does most of the parsing for us.
  1232.      First, get its vectors set up.  */
  1233.  
  1234.   init_switches ();
  1235.  
  1236.   /* Let getopt produce error messages for the command line,
  1237.      but not for options from the environment.  */
  1238.   opterr = !env;
  1239.   /* Reset getopt's state.  */
  1240.   optind = 0;
  1241.  
  1242.   while ((c = getopt_long (argc, argv,
  1243.                options, long_options, (int *) 0)) != EOF)
  1244.     {
  1245.       if (c == '?')
  1246.     /* Bad option.  We will print a usage message and die later.
  1247.        But continue to parse the other options so the user can
  1248.        see all he did wrong.  */
  1249.     bad = 1;
  1250.       else
  1251.     for (cs = switches; cs->c != '\0'; ++cs)
  1252.       if (cs->c == c)
  1253.         {
  1254.           /* Whether or not we will actually do anything with
  1255.          this switch.  We test this individually inside the
  1256.          switch below rather than just once outside it, so that
  1257.          options which are to be ignored still consume args.  */
  1258.           int doit = !env || cs->env;
  1259.  
  1260.           switch (cs->type)
  1261.         {
  1262.         default:
  1263.           abort ();
  1264.  
  1265.         case ignore:
  1266.           break;
  1267.  
  1268.         case flag:
  1269.         case flag_off:
  1270.           if (doit)
  1271.             *(int *) cs->value_ptr = cs->type == flag;
  1272.           break;
  1273.  
  1274.         case string:
  1275.           if (!doit)
  1276.             break;
  1277.  
  1278.           if (optarg == 0)
  1279.             optarg = cs->noarg_value;
  1280.  
  1281.           sl = *(struct stringlist **) cs->value_ptr;
  1282.           if (sl == 0)
  1283.             {
  1284.               sl = (struct stringlist *)
  1285.             xmalloc (sizeof (struct stringlist));
  1286.               sl->max = 5;
  1287.               sl->idx = 0;
  1288.               sl->list = (char **) xmalloc (5 * sizeof (char *));
  1289.               *(struct stringlist **) cs->value_ptr = sl;
  1290.             }
  1291.           else if (sl->idx == sl->max - 1)
  1292.             {
  1293.               sl->max += 5;
  1294.               sl->list = (char **)
  1295.             xrealloc ((char *) sl->list,
  1296.                   sl->max * sizeof (char *));
  1297.             }
  1298.           sl->list[sl->idx++] = optarg;
  1299.           sl->list[sl->idx] = 0;
  1300.           break;
  1301.  
  1302.         case positive_int:
  1303.           if (optarg == 0 && argc > optind
  1304.               && isdigit (argv[optind][0]))
  1305.             optarg = argv[optind++];
  1306.  
  1307.           if (!doit)
  1308.             break;
  1309.  
  1310.           if (optarg != 0)
  1311.             {
  1312.               int i = atoi (optarg);
  1313.               if (i < 1)
  1314.             {
  1315.               if (doit)
  1316.                 error ("the `-%c' option requires a \
  1317. positive integral argument",
  1318.                    cs->c);
  1319.               bad = 1;
  1320.             }
  1321.               else
  1322.             *(unsigned int *) cs->value_ptr = i;
  1323.             }
  1324.           else
  1325.             *(unsigned int *) cs->value_ptr
  1326.               = *(unsigned int *) cs->noarg_value;
  1327.           break;
  1328.  
  1329.         case floating:
  1330.           if (optarg == 0 && optind < argc
  1331.               && (isdigit (argv[optind][0]) || argv[optind][0] == '.'))
  1332.             optarg = argv[optind++];
  1333.  
  1334.           if (doit)
  1335.             *(double *) cs->value_ptr            
  1336.               = (optarg != 0 ? atof (optarg)
  1337.              : *(double *) cs->noarg_value);
  1338.  
  1339.           break;
  1340.         }
  1341.         
  1342.           /* We've found the switch.  Stop looking.  */
  1343.           break;
  1344.         }
  1345.     }
  1346.  
  1347.   if (!env)
  1348.     {
  1349.       /* Collect the remaining args in the `other_args' string list.  */
  1350.  
  1351.       while (optind < argc)
  1352.     {
  1353.       char *arg = argv[optind++];
  1354.       if (arg[0] != '-' || arg[1] != '\0')
  1355.         other_args->list[other_args->idx++] = arg;
  1356.     }
  1357.       other_args->list[other_args->idx] = 0;
  1358.     }
  1359.  
  1360.   if (!env && (bad || print_usage_flag))
  1361.     {
  1362.       /* Print a nice usage message.  */
  1363.  
  1364.     print_version ();
  1365.  
  1366.       fprintf (stderr, "Usage: %s [options] [target] ...\n", program);
  1367.  
  1368.       fputs ("Options:\n", stderr);
  1369.       for (cs = switches; cs->c != '\0'; ++cs)
  1370.     {
  1371.       char buf[1024], shortarg[50], longarg[50], *p;
  1372.  
  1373.       if (cs->description[0] == '-')
  1374.         continue;
  1375.  
  1376.       switch (long_options[cs - switches].has_arg)
  1377.         {
  1378.         case no_argument:
  1379.           shortarg[0] = longarg[0] = '\0';
  1380.           break;
  1381.         case required_argument:
  1382.           sprintf (longarg, "=%s", cs->argdesc);
  1383.           sprintf (shortarg, " %s", cs->argdesc);
  1384.           break;
  1385.         case optional_argument:
  1386.           sprintf (longarg, "[=%s]", cs->argdesc);
  1387.           sprintf (shortarg, " [%s]", cs->argdesc);
  1388.           break;
  1389.         }
  1390.  
  1391.       p = buf;
  1392.  
  1393.       if (isalnum (cs->c))
  1394.         {
  1395.           sprintf (buf, "  -%c%s", cs->c, shortarg);
  1396.           p += strlen (p);
  1397.         }
  1398.       if (cs->long_name != 0)
  1399.         {
  1400.           unsigned int i;
  1401.           sprintf (p, "%s--%s%s",
  1402.                !isalnum (cs->c) ? "  " : ", ",
  1403.                cs->long_name, longarg);
  1404.           p += strlen (p);
  1405.           for (i = 0; i < (sizeof (long_option_aliases) /
  1406.                    sizeof (long_option_aliases[0]));
  1407.            ++i)
  1408.         if (long_option_aliases[i].val == cs->c)
  1409.           {
  1410.             sprintf (p, ", --%s%s",
  1411.                  long_option_aliases[i].name, longarg);
  1412.             p += strlen (p);
  1413.           }
  1414.         }
  1415.       {
  1416.         const struct command_switch *ncs = cs;
  1417.         while ((++ncs)->c != '\0')
  1418.           if (ncs->description[0] == '-' &&
  1419.           ncs->description[1] == cs->c)
  1420.         {
  1421.           /* This is another switch that does the same
  1422.              one as the one we are processing.  We want
  1423.              to list them all together on one line.  */
  1424.           sprintf (p, ", -%c%s", ncs->c, shortarg);
  1425.           p += strlen (p);
  1426.           if (ncs->long_name != 0)
  1427.             {
  1428.               sprintf (p, ", --%s%s", ncs->long_name, longarg);
  1429.               p += strlen (p);
  1430.             }
  1431.         }
  1432.       }
  1433.  
  1434.       if (p - buf > DESCRIPTION_COLUMN - 2)
  1435.         /* The list of option names is too long to fit on the same
  1436.            line with the description, leaving at least two spaces.
  1437.            Print it on its own line instead.  */
  1438.         {
  1439.           fprintf (stderr, "%s\n", buf);
  1440.           buf[0] = '\0';
  1441.         }
  1442.  
  1443.       fprintf (stderr, "%*s%s.\n",
  1444.            - DESCRIPTION_COLUMN,
  1445.            buf, cs->description);
  1446.     }
  1447.  
  1448.       die (bad ? 2 : 0);
  1449.     }
  1450. }
  1451.  
  1452. /* Decode switches from environment variable ENVAR (which is LEN chars long).
  1453.    We do this by chopping the value into a vector of words, prepending a
  1454.    dash to the first word if it lacks one, and passing the vector to
  1455.    decode_switches.  */
  1456.  
  1457. static void
  1458. decode_env_switches (envar, len)
  1459.      char *envar;
  1460.      unsigned int len;
  1461. {
  1462.   char *varref = (char *) alloca (2 + len + 2);
  1463.   char *value, *args;
  1464.   int argc;
  1465.   char **argv;
  1466.  
  1467.   /* Get the variable's value.  */
  1468.   varref[0] = '$';
  1469.   varref[1] = '(';
  1470.   bcopy (envar, &varref[2], len);
  1471.   varref[2 + len] = ')';
  1472.   varref[2 + len + 1] = '\0';
  1473.   value = variable_expand (varref);
  1474.  
  1475.   /* Skip whitespace, and check for an empty value.  */
  1476.   value = next_token (value);
  1477.   len = strlen (value);
  1478.   if (len == 0)
  1479.     return;
  1480.  
  1481.   /* Make a copy of the value in ARGS, where we will munge it.
  1482.      If it does not begin with a dash, prepend one.
  1483.      We must allocate lasting storage for this (and we never free it) because
  1484.      decode_switches may save pointers into it for string-valued switches.  */
  1485.   args = (char *) xmalloc (1 + len + 2);
  1486.   if (value[0] != '-')
  1487.     args[0] = '-';
  1488.   bcopy (value, value[0] == '-' ? args : &args[1], len + 1);
  1489.   /* Write an extra null terminator so our loop below will
  1490.      never be in danger of looking past the end of the string.  */
  1491.   args[(value[0] == '-' ? 0 : 1) + len + 1] = '\0';
  1492.  
  1493.   /* Allocate a vector that is definitely big enough.  */
  1494.   argv = (char **) alloca ((1 + len + 1) * sizeof (char *));
  1495.  
  1496.   /* getopt will look at the arguments starting at ARGV[1].
  1497.      Prepend a spacer word.  */
  1498.   argv[0] = 0;
  1499.   argc = 1;
  1500.   do
  1501.     {
  1502.       argv[argc++] = args;
  1503.       args = end_of_token (args);
  1504.       *args++ = '\0';
  1505.     } while (*args != '\0');
  1506.   argv[argc] = 0;
  1507.  
  1508.   /* Parse those words.  */
  1509.   decode_switches (argc, argv, 1);
  1510. }
  1511.  
  1512. /* Define the MAKEFLAGS and MFLAGS variables to reflect the settings of the
  1513.    command switches.  Include options with args if ALL is nonzero.
  1514.    Don't include options with the `no_makefile' flag set if MAKEFILE.  */
  1515.  
  1516. static void
  1517. define_makeflags (all, makefile)
  1518.      int all, makefile;
  1519. {
  1520.   register const struct command_switch *cs;
  1521.   char *flagstring;
  1522.   struct variable *v;
  1523.  
  1524.   /* We will construct a linked list of `struct flag's describing
  1525.      all the flags which need to go in MAKEFLAGS.  Then, once we
  1526.      know how many there are and their lengths, we can put them all
  1527.      together in a string.  */
  1528.  
  1529.   struct flag
  1530.     {
  1531.       struct flag *next;
  1532.       const struct command_switch *cs;
  1533.       char *arg;
  1534.       unsigned int arglen;
  1535.     };
  1536.   struct flag *flags = 0;
  1537.   unsigned int flagslen = 0;
  1538. #define    ADD_FLAG(ARG, LEN) \
  1539.   do {                                          \
  1540.     struct flag *new = (struct flag *) alloca (sizeof (struct flag));          \
  1541.     new->cs = cs;                                  \
  1542.     new->arg = (ARG);                                  \
  1543.     new->arglen = (LEN);                              \
  1544.     new->next = flags;                                  \
  1545.     flags = new;                                  \
  1546.     if (new->arg == 0)                                  \
  1547.       ++flagslen;        /* Just a single flag letter.  */          \
  1548.     else                                      \
  1549.       flagslen += 1 + 1 + 1 + 1 + new->arglen; /* " -x foo" */              \
  1550.     if (!isalnum (cs->c))                              \
  1551.       /* This switch has no single-letter version, so we use the long.  */    \
  1552.       flagslen += 2 + strlen (cs->long_name);                      \
  1553.   } while (0)
  1554.  
  1555.   for (cs = switches; cs->c != '\0'; ++cs)
  1556.     if (cs->toenv && (!makefile || !cs->no_makefile))
  1557.       switch (cs->type)
  1558.     {
  1559.     default:
  1560.       abort ();
  1561.  
  1562.     case ignore:
  1563.       break;
  1564.  
  1565.     case flag:
  1566.     case flag_off:
  1567.       if (!*(int *) cs->value_ptr == (cs->type == flag_off)
  1568.           && (cs->default_value == 0
  1569.           || *(int *) cs->value_ptr != *(int *) cs->default_value))
  1570.         ADD_FLAG (0, 0);
  1571.       break;
  1572.  
  1573.     case positive_int:
  1574.       if (all)
  1575.         {
  1576.           if ((cs->default_value != 0
  1577.            && (*(unsigned int *) cs->value_ptr
  1578.                == *(unsigned int *) cs->default_value)))
  1579.         break;
  1580.           else if (cs->noarg_value != 0
  1581.                && (*(unsigned int *) cs->value_ptr ==
  1582.                *(unsigned int *) cs->noarg_value))
  1583.         ADD_FLAG ("", 0); /* Optional value omitted; see below.  */
  1584.           else if (cs->c == 'j')
  1585.         /* Special case for `-j'.  */
  1586.         ADD_FLAG ("1", 1);
  1587.           else
  1588.         {
  1589.           char *buf = (char *) alloca (30);
  1590.           sprintf (buf, "%u", *(unsigned int *) cs->value_ptr);
  1591.           ADD_FLAG (buf, strlen (buf));
  1592.         }
  1593.         }
  1594.       break;
  1595.  
  1596.     case floating:
  1597.       if (all)
  1598.         {
  1599.           if (cs->default_value != 0
  1600.           && (*(double *) cs->value_ptr
  1601.               == *(double *) cs->default_value))
  1602.         break;
  1603.           else if (cs->noarg_value != 0
  1604.                && (*(double *) cs->value_ptr
  1605.                == *(double *) cs->noarg_value))
  1606.         ADD_FLAG ("", 0); /* Optional value omitted; see below.  */
  1607.           else
  1608.         {
  1609.           char *buf = (char *) alloca (100);
  1610.           sprintf (buf, "%g", *(double *) cs->value_ptr);
  1611.           ADD_FLAG (buf, strlen (buf));
  1612.         }
  1613.         }
  1614.       break;
  1615.  
  1616.     case string:
  1617.       if (all)
  1618.         {
  1619.           struct stringlist *sl = *(struct stringlist **) cs->value_ptr;
  1620.           if (sl != 0)
  1621.         {
  1622.           /* Add the elements in reverse order, because
  1623.              all the flags get reversed below; and the order
  1624.              matters for some switches (like -I).  */
  1625.           register unsigned int i = sl->idx;
  1626.           while (i-- > 0)
  1627.             ADD_FLAG (sl->list[i], strlen (sl->list[i]));
  1628.         }
  1629.         }
  1630.       break;
  1631.     }
  1632.  
  1633. #undef    ADD_FLAG
  1634.  
  1635.   if (flags == 0)
  1636.     /* No flags.  Use a string of two nulls so [1] works below.  */
  1637.     flagstring = "\0";
  1638.   else
  1639.     {
  1640.       /* Construct the value in FLAGSTRING.
  1641.      We allocate enough space for a preceding dash and trailing null.  */
  1642.       register char *p;
  1643.       flagstring = (char *) alloca (1 + flagslen + 1);
  1644.       p = flagstring;
  1645.       *p++ = '-';
  1646.       do
  1647.     {
  1648.       /* Add the flag letter or name to the string.  */
  1649.       if (!isalnum (flags->cs->c))
  1650.         {
  1651.           *p++ = '-';
  1652.           strcpy (p, flags->cs->long_name);
  1653.           p += strlen (p);
  1654.         }
  1655.       else
  1656.         *p++ = flags->cs->c;
  1657.       if (flags->arg != 0)
  1658.         {
  1659.           /* A flag that takes an optional argument which in this case
  1660.          is omitted is specified by ARG being "" and ARGLEN being 0.
  1661.          We must distinguish because a following flag appended without
  1662.          an intervening " -" is considered the arg for the first.  */
  1663.           if (flags->arglen > 0)
  1664.         {
  1665.           /* Add its argument too.  */
  1666.           *p++ = !isalnum (flags->cs->c) ? '=' : ' ';
  1667.           bcopy (flags->arg, p, flags->arglen);
  1668.           p += flags->arglen;
  1669.         }
  1670.           /* Write a following space and dash, for the next flag.  */
  1671.           *p++ = ' ';
  1672.           *p++ = '-';
  1673.         }
  1674.       else if (!isalnum (flags->cs->c))
  1675.         {
  1676.           /* Long options must each go in their own word,
  1677.          so we write the following space and dash.  */
  1678.           *p++ = ' ';
  1679.           *p++ = '-';
  1680.         }
  1681.       flags = flags->next;
  1682.     } while (flags != 0);
  1683.  
  1684.       if (p[-1] == '-')
  1685.     /* Kill the final space and dash.  */
  1686.     p -= 2;
  1687.  
  1688.       /* Terminate the string.  */
  1689.       *p = '\0';
  1690.     }
  1691.  
  1692.   v = define_variable ("MAKEFLAGS", 9,
  1693.                /* On Sun, the value of MFLAGS starts with a `-' but
  1694.               the value of MAKEFLAGS lacks the `-'.
  1695.               Be compatible with this unless FLAGSTRING starts
  1696.               with a long option `--foo', since removing the
  1697.               first dash would result in the bogus `-foo'.  */
  1698.                flagstring[1] == '-' ? flagstring : &flagstring[1],
  1699.                /* This used to use o_env, but that lost when a
  1700.               makefile defined MAKEFLAGS.  Makefiles set
  1701.               MAKEFLAGS to add switches, but we still want
  1702.               to redefine its value with the full set of
  1703.               switches.  Of course, an override or command
  1704.               definition will still take precedence.  */
  1705.                o_file, 0);
  1706.   if (! all)
  1707.     /* The first time we are called, set MAKEFLAGS to always be exported.
  1708.        We should not do this again on the second call, because that is
  1709.        after reading makefiles which might have done `unexport MAKEFLAGS'. */
  1710.     v->export = v_export;
  1711.   /* Since MFLAGS is not parsed for flags, there is no reason to
  1712.      override any makefile redefinition.  */
  1713.   (void) define_variable ("MFLAGS", 6, flagstring, o_env, 0);
  1714. }
  1715.  
  1716. /* Print version information.  */
  1717.  
  1718. static void
  1719. print_version ()
  1720. {
  1721.   static int printed_version = 0;
  1722.  
  1723.   char *precede = print_data_base_flag ? "# " : "";
  1724.  
  1725.   if (printed_version)
  1726.     /* Do it only once.  */
  1727.     return;
  1728.  
  1729.   printf ("%sGNU Make version %s", precede, version_string);
  1730.   if (remote_description != 0 && *remote_description != '\0')
  1731.     printf ("-%s", remote_description);
  1732.  
  1733.   printf (", by Richard Stallman and Roland McGrath.\n\
  1734. %sCopyright (C) 1988, 89, 90, 91, 92, 93, 94 Free Software Foundation, Inc.\n\
  1735. %sThis is free software; see the source for copying conditions.\n\
  1736. %sThere is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A\n\
  1737. %sPARTICULAR PURPOSE.\n\n", precede, precede, precede, precede);
  1738.  
  1739.   printed_version = 1;
  1740.  
  1741.   /* Flush stdout so the user doesn't have to wait to see the
  1742.      version information while things are thought about.  */
  1743.   fflush (stdout);
  1744. }
  1745.  
  1746. /* Print a bunch of information about this and that.  */
  1747.  
  1748. static void
  1749. print_data_base ()
  1750. {
  1751.   extern char *ctime ();
  1752.   time_t when;
  1753.  
  1754.   when = time ((time_t *) 0);
  1755.   printf ("\n# Make data base, printed on %s", ctime (&when));
  1756.  
  1757.   print_variable_data_base ();
  1758.   print_dir_data_base ();
  1759.   print_rule_data_base ();
  1760.   print_file_data_base ();
  1761.   print_vpath_data_base ();
  1762.  
  1763.   when = time ((time_t *) 0);
  1764.   printf ("\n# Finished Make data base on %s\n", ctime (&when));
  1765. }
  1766.  
  1767. /* Exit with STATUS, cleaning up as necessary.  */
  1768.  
  1769. void
  1770. die (status)
  1771.      int status;
  1772. {
  1773.   static char dying = 0;
  1774.  
  1775.   if (!dying)
  1776.     {
  1777.       int err;
  1778.  
  1779.       dying = 1;
  1780.  
  1781.       if (print_version_flag)
  1782.     print_version ();
  1783.  
  1784.       /* Remove the intermediate files.  */
  1785.       remove_intermediates (0);
  1786.  
  1787.       /* Wait for children to die.  */
  1788.       for (err = status != 0; job_slots_used > 0; err = 0)
  1789.     reap_children (1, err);
  1790.  
  1791.       if (print_data_base_flag)
  1792.     print_data_base ();
  1793.  
  1794.       if (print_directory_flag)
  1795.     log_working_directory (0);
  1796.     }
  1797.  
  1798.   exit (status);
  1799. }
  1800.  
  1801. /* Write a message indicating that we've just entered or
  1802.    left (according to ENTERING) the current directory.  */
  1803.  
  1804. static void
  1805. log_working_directory (entering)
  1806.      int entering;
  1807. {
  1808.   static int entered = 0;
  1809.   char *message = entering ? "Entering" : "Leaving";
  1810.  
  1811.   if (entering)
  1812.     entered = 1;
  1813.   else if (!entered)
  1814.     /* Don't print the leaving message if we
  1815.        haven't printed the entering message.  */
  1816.     return;
  1817.  
  1818.   if (print_data_base_flag)
  1819.     fputs ("# ", stdout);
  1820.  
  1821.   if (makelevel == 0)
  1822.     printf ("%s: %s ", program, message);
  1823.   else
  1824.     printf ("%s[%u]: %s ", program, makelevel, message);
  1825.  
  1826.   if (starting_directory == 0)
  1827.     puts ("an unknown directory");
  1828.   else
  1829.     printf ("directory `%s'\n", starting_directory);
  1830. }
  1831.